package dao

import (
	"time"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

var DB *gorm.DB

func init() {
	//DB = new(gorm.DB)
	DB, _ = LoadDb()
	if DB == nil {

	}
}

func LoadDb() (db *gorm.DB, err error) {
	//db, err = gorm.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True")
	db, err = gorm.Open("mysql", "star:starUS45d#@dkf@tcp(star-stage-mysql:3306)/star_mall?charset=utf8mb4&parseTime=True")
	if err != nil {
		panic(err.Error())
	} else {
		db.DB().SetConnMaxLifetime(time.Second * time.Duration(10))
		db.DB().SetMaxOpenConns(20)
		db.DB().SetMaxIdleConns(10)
		//db.SetLogger(logger.Logger)
		db.LogMode(true)
		db.Debug()
		// 如果设置禁用表名复数形式属性为 true，`User` 的表名将是 `user`
		db.SingularTable(true)
	}
	return db, err
}
